<?= $this->Html->script('production', array('block'=>'script')) ?>
<script type="text/javascript">
var tax_values = <?= json_encode($tax_values) ?>;
function storeChanged(store_id) {
	loadStoreRabat(store_id, $('#ProductionDate').val(), $('#product_prices input.rabat'), function(rabat){
		UpdatePrices();
	});
}
function carrierChanged() {
	var carrier_id = $('#ProductionCarrierId').val();
	var vehicles   = $('#ProductionVehicleId');
	var drivers    = $('#ProductionDriverId');
	
	var vehicle_id = parseInt('<?= @$data['Production']['vehicle_id'] ?>'); 
	vehicle_id = vehicle_id>0 ? vehicle_id : 0;
	
	var driver_id = parseInt('<?= @$data['Production']['driver_id'] ?>'); 
	driver_id = driver_id>0 ? driver_id : 0;
	
	$.ajax({
		url: '/proizvodnja/prevoznik_data/' + carrier_id,
		beforeSend: function(){
			vehicles.empty().attr('disabled', 'disabled');
			drivers.empty().attr('disabled', 'disabled');
		},
		complete: function(req) {
			vehicles.removeAttr('disabled');
			drivers.removeAttr('disabled');
			if(req.status==200){
				var data = eval('('+req.responseText+')');
				if(data.vehicles) {
					for(id in data.vehicles) {
						var sel = id==vehicle_id ? ' selected="selected"' : '';
						vehicles.append('<option value="'+id+'"'+sel+'>'+data.vehicles[id]+'</option>');
					}
				}
				if(data.drivers) {
					for(id in data.drivers) {
						var sel = id==driver_id ? ' selected="selected"' : '';
						drivers.append('<option value="'+id+'"'+sel+'>'+data.drivers[id]+'</option>');
					}
				}
			}
		}
	});
}
$(function(){
    var store_id = $('#ProductionStoreId').val();
    
	reloadPricelist();
	loadTax($('#ProductionDate').val(), $('#tax-value'));
	$('#ProductionDate').change(function(){
		reloadPricelist();
		storeChanged($('#ProductionStoreId').val());
		loadTax($(this).val(), $('#tax-value'));
	});

	$('#product_prices input,#product_prices select').change(UpdatePrices);
	
	carrierChanged();
});
</script>

<?php $type = $data['Production']['type'] ?>

<p><?= $this->Html->link('&laquo; Nazad na listu ', '/proizvodnja/' . Inflector::slug($type), array('escape'=>false)) ?></p>
<style type="text/css">
	#store_id-autocomplete-container ul{width:300px}
	.price{display:block;float:left;width:80px;text-align:right;margin-right:5px;font-size:11px;margin:7px 10px 7px 5px}
	.price:after,.price-total span:after{content:" RSD";font-size:9px}
	.price-total{margin:10px 0 0;border-top:3px double #555;padding:3px;width:100%;text-align:right;font-weight:normal;font-size:11px;text-transform:uppercase}
	.price-total span{font-weight:bolder}
</style>

<?= $this->Form->create('Production', array('url'=>'/proizvodnja/sacuvaj', 'class'=>'entity-form', 'style'=>'width:600px')) ?>
	<?= $this->Form->hidden('id', array('value'=>@$data['Production']['id'])) ?>
	<?= $this->Form->hidden('type', array('value'=>@$data['Production']['type'])) ?>
	
	<?= $this->Form->label('number', 'Broj:') ?>
	<?= $this->Form->text('number', array(
		'value' => @$data['Production']['number']
	)) ?>
	
	<?= $this->Form->label('order_number', 'Porudžbenica:') ?>
	<?= $this->Form->text('order_number', array(
		'value' => @$data['Production']['order_number'],
		'class' => 'medium'
	)) ?>

	<?= $this->Form->label('date', 'Datum otpreme:') ?>
	<?= $this->Form->text('date', array(
		'value' => (empty($data['Production']['date']) ? date('Y-m-d') : $data['Production']['date']), 
		'class' => 'datepicker'
	)) ?>
	
	<?= $this->Form->label('date_trade', 'Datum prometa:', array('class'=>'inline')) ?>
	<?= $this->Form->text('date_trade', array(
		'value' => (empty($data['Production']['date_trade']) ? date('Y-m-d') : $data['Production']['date_trade']), 
		'class' => 'datepicker'
	)) ?>
	
	<?= $this->Form->label('date_payment', 'Rok za plaćanje:', array('class'=>'inline')) ?>
	<?= $this->Form->text('date_payment', array(
		'value' => (empty($data['Production']['date_payment']) ? date('Y-m-d') : $data['Production']['date_payment']), 
		'class' => 'datepicker'
	)) ?>
	
	<?= $this->element('store_picker', array(
	    'name'   => 'store_id',
        'label'  => 'Prodavnica:',
	    'value'  => @$data['Store'],
	    'select' => 'storeChanged'
	)) ?>
	<span class="error-message-conatiner"><?= $this->Form->error('store_id') ?></span>
	
	<?= $this->Form->label('porez', 'Porez:') ?>
	<div class="value text-right" id="tax-value"></div><span class="left" style="margin:7px 0">%</span>
	
	<hr>
	<?= $this->Form->label('carrier_id', 'Prevoznik:') ?>
	<?= $this->Form->select('carrier_id', $carriers, array(
		'value'    => @$data['Production']['carrier_id'], 
		'empty'    => false,
		'onchange' => 'carrierChanged();'
	)) ?>
	<?= $this->Form->select('driver_id', array(), array('empty'=>false)) ?>
	<?= $this->Form->select('vehicle_id', array(), array('empty'=>false)) ?>
	
	<?= $this->Form->label('comment', 'Napomena:') ?>
	<?= $this->Form->textarea('comment', array('value' => @$data['Production']['comment'])) ?>
	
	<hr>
	<?= $this->Form->label('ProductionProduct', 'Proizvodi:') ?>
	<div class="left multi-inputs" id="product_prices">
		<? if(empty($data['ProductionProduct'])){
			$data['ProductionProduct'] = array(
				array(
					'amount' => 1,
					'product_id' => '',
					'price' => ''
				)
			);
		} ?>
		<? foreach($data['ProductionProduct'] as $k=>$production_product): ?>
		<? 
		    $tax_value = empty($production_product['tax']) ? $tax : $production_product['tax'];
		?>
		<div class="item">
		    <?= $this->Form->hidden("ProductionProduct.{$k}.price", array('value'=>@$production_product['price'])) ?>
		    
			<?= $this->Form->text("ProductionProduct.{$k}.amount", array('placeholder'=>'Kol.', 'class'=>'small amount', 'value'=>(float)$production_product['amount'])) ?>
			<?= $this->Form->select("ProductionProduct.{$k}.product_id", $products, array('empty'=>false, 'value'=>@$production_product['product_id'])) ?>
		    <?= $this->Form->text("ProductionProduct.{$k}.rabat", array('placeholder'=>'Rabat', 'class'=>'small rabat', 'value'=>@$production_product['rabat'])) ?>
		    <span class="value">%</span>
			<span class="price"><?= $production_product['price'] ?></span>
		</div>
		<? endforeach; ?>
	</div>
	<span class="error-message-conatiner"><?= $this->Form->error('ProductionProduct.0.product_id') ?></span>
	<strong class="price-total right clear">Total: <span></span></strong>		
	
	
	
	<?= $this->Form->submit('Sačuvaj', array('div'=>false)) ?>
<?= $this->Form->end() ?>